//
//  LCVideoClassifiesMoreVC.m
//  JKFuYinProject
//
//  Created by 九酷 on 2018/3/5.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LCVideoClassifiesMoreVC.h"
#import "LSZMoveCollectionViewCell.h"
#import "LCVideoHomeListAboutModel.h"
#import "LCVideoHomeModel.h"
#import "VideoCollectionViewCell.h"

@interface LCVideoClassifiesMoreVC ()

@end

@implementation LCVideoClassifiesMoreVC
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
-(void)setDataDic :(NSInteger)page {
    [self feileixiangqing:page];//等
}

//将首页的前10条数据赋值到 beautifulEssayListArray 属性里
- (void)setupPageOneData:(NSArray *)dictArr2 {
    NSMutableArray *mArray = [[NSMutableArray alloc] initWithArray:dictArr2];
    self.mutArrayData = mArray;
}

#pragma mark - 分类等
-(void)feileixiangqing :(NSInteger)page
{
//    [LSZRequestResolutionDic VideoHomeTypeClassification:self.strTitleNews page:[NSString stringWithFormat:@"%zd",page] :^(BOOL isOK, NSString * str, LSZVideoUpdateListManager * mg) {
//        if (isOK == YES) {
//            if (self.page == 1) {
//                [self.mutArrayData  removeAllObjects];
//                [self.mutArrayData addObjectsFromArray:mg.arrayData];
//            }else{
//                [self.mutArrayData addObjectsFromArray:mg.arrayData];
//            }
//            if(mg.arrayData.count == 0){
//                [self.collectionViewMove.footer noticeNoMoreData];
//            }
//            [self.collectionViewMove reloadData];
//        }else{
//            [self setStringWarning:str];
//        }
//    }];
    NSDictionary *diction = [[NSDictionary alloc]initWithObjectsAndKeys:self.type,@"type",[NSString stringWithFormat:@"%ld",(long)page],@"page",count_num30,@"size", nil];
    NSString *url = [NSString stringWithFormat:@"%@/Movie/catelist",http_zongSJUrl];
    [kNetworkTool POSTFuYinWithUrlString:url input:diction success:^(NSDictionary * dic) {
        NSString *status = [NSString stringWithFormat:@"%@",dic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            NSArray *dictArr2;
            @try {
                dictArr2 = dic[@"data"][@"list"];
            } @catch (NSException *exception) {
            }
            if (self.page == 1) {
                NSArray *videoManager = [NSArray yy_modelArrayWithClass:[LCVideoHomeListAboutModel class] json:dictArr2];
                [self setupPageOneData:videoManager];
                if (videoManager != nil) {
                    [self.collectionViewMove reloadData];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.41 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [self.collectionViewMove.mj_header endRefreshing];
                    });
                }
            }else{
                NSMutableArray *mArray = [NSArray yy_modelArrayWithClass:[LCVideoHomeListAboutModel class] json:dictArr2].mutableCopy;
                if (mArray.count > 0) {
                    [self.mutArrayData addObjectsFromArray:mArray];
                }
                if (mArray.count > 0) {
                    [self.collectionViewMove reloadData];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.41 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [self.collectionViewMove.mj_footer endRefreshing];
                    });
                }
                else {
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.41 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [self.collectionViewMove.mj_footer endRefreshingWithNoMoreData];
                    });
                }
            }

        }else{
            NSString * msg = [NSString stringWithFormat:@"%@",dic[@"msg"]];
            NSLog(@"msg = %@", msg);
        }
    } failure:^(NSError * error) {
        [self.collectionViewMove.mj_header endRefreshing];
        [self.collectionViewMove.mj_footer endRefreshing];
//        [LSZSpecialPackaging Reachability:^(NSString * strMessage) {
//        }];
    }];
}

//#pragma mark - 某个牧师的列表
//-(void)aListOfPriests :(NSInteger)page
//{
//    [LSZRequestResolutionDic detailsOfPriestisWork:self.name  page:[NSString stringWithFormat:@"%zd",page] :^(BOOL isOK, NSString * str, LSZVideoUpdateListManager * mg) {
//        if (isOK == YES) {
//            if (self.page == 1) {
//                [self.mutArrayData  removeAllObjects];
//                [self.mutArrayData addObjectsFromArray:mg.arrayData];
//            }else{
//                [self.mutArrayData addObjectsFromArray:mg.arrayData];
//            }
//            if(mg.arrayData.count == 0){
//                [self.collectionViewMove.mj_footer noticeNoMoreData];
//            }
//            [self.collectionViewMove reloadData];
//        }else{
//            [self setStringWarning:str];
//        }
//    }];
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.strTitle;
    [self setUICollection];
    
    __weak typeof(self) weakSelf = self;
    self.collectionViewMove.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        weakSelf.page = 1;
        //诗歌歌谱的分类
        [weakSelf setDataDic:weakSelf.page];
    }];
    // 上拉刷新
    self.collectionViewMove.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        weakSelf.page++;
        //诗歌歌谱的分类
        [weakSelf setDataDic:weakSelf.page];
    }];
    self.page = 1;
    [self setDataDic:self.page];
}
-(void)setUICollection
{
    CGFloat width = (ScreenWidth - 40) / 3;
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(width,width+30+60);
    flowLayout.minimumInteritemSpacing = 5;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(10,10,10,10);
    self.collectionViewMove = [[UICollectionView alloc]initWithFrame:CGRectMake(0,0, ScreenWidth, ScreenHeight) collectionViewLayout:flowLayout];
    self.collectionViewMove.delegate   = self;
    self.collectionViewMove.dataSource = self;
    self.collectionViewMove.backgroundColor = RGBColor(243, 243, 243);
    [self.view addSubview:self.collectionViewMove];

    [self.collectionViewMove registerClass:[VideoCollectionViewCell class] forCellWithReuseIdentifier:@"LSZTabbarVideoCollectionViewCell"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.mutArrayData.count;
}
#pragma mark - 单元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    VideoCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LSZTabbarVideoCollectionViewCell" forIndexPath:indexPath];
    LCVideoHomeListAboutModel *model = self.mutArrayData[indexPath.row];

    cell.viewLow.backgroundColor = RGBColor(243, 243, 243);
    [cell.imageViewIcon mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cell.mas_top).offset(0);
        make.left.equalTo(cell.mas_left);
        make.right.equalTo(cell.mas_right);
        make.height.mas_equalTo(cell.imageViewIcon.mas_width).multipliedBy(1.33);
    }];

    [cell.imageViewIcon  sd_setImageWithURL:[NSURL URLWithString:model.pic] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    if (![model.name isEqualToString:@""]) {
        cell.titleLabel.text = [NSString stringWithFormat:@"%@",model.name];
    }else {
        cell.titleLabel.text = [NSString stringWithFormat:@"%@",model.author];
    }
    cell.backgroundColor = RGBColor(243, 243, 243);
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
        LCVideoHomeListAboutModel *listAboutModel = self.mutArrayData[indexPath.row];
        NSLog(@"listAboutModel.id = %@", listAboutModel.id);
        LCVideoDetailChangeHeaderController *videoDetailChangeHeaderController = [[LCVideoDetailChangeHeaderController alloc] init];
        videoDetailChangeHeaderController.listAboutModel = listAboutModel;
        [self.navigationController pushViewController:videoDetailChangeHeaderController animated:YES];

        NSArray * arrayGetData = [[NSArray alloc]initWithContentsOfFile:[LSZSpecialPackaging recordTheBrowsingHistoryVideo]];
        for (NSDictionary * dic in arrayGetData) {
            NSString * name = NSStr(dic[@"id_id"]);
            if ([name isEqualToString:listAboutModel.id]) {
                return;
            }
        }
        //还没有添加过
        NSMutableArray * mutArrayAdd = [[NSMutableArray alloc]initWithArray:arrayGetData];
        NSMutableDictionary * mutDic = [[NSMutableDictionary alloc]initWithObjectsAndKeys:listAboutModel.name,@"title",listAboutModel.pic,@"imageViewUrl",listAboutModel.author,@"name",listAboutModel.id,@"id_id",nil];
        [mutArrayAdd addObject:mutDic];
        [mutArrayAdd writeToFile:[LSZSpecialPackaging recordTheBrowsingHistoryVideo] atomically:YES];
    //历史记录有问题
}
@end
